<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <!-- 添加一级部门一级部门的pid都是'' -->
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
          :default-expand-all="true"
        >
          <!-- 作用域插槽 data拿到的是每一个子节点的对象 -->
          <!--
      #default === slot-scope
      什么时候用到作用域插槽？父组件中如果想使用子组件中的数据进行自定义内容的渲染 (类似于table 单元格数据渲染)
    -->
          <template v-slot="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <!-- slot="dropdown"可以拿到作用域插槽里data -->
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <!-- 如果有子目录就不显示删除功能 -->
                        <el-dropdown-item v-if="!data.children.length" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <!-- <el-tree :data="list" :props="{label: 'name'}" :default-expand-all="true" /> -->
      </el-card>
    </div>

    <!-- Dialog弹窗 -->
    <el-dialog
      :title="isEdit ? '编辑' : '添加'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="50%"
    >
      <!-- 使用v-if重置表单 每次关闭dialog都会销毁子组件 -->
      <add-or-edit v-if="showDialog" :id="curId" :origin-list="originList" :is-edit="isEdit" @close="hClose" @success="hSuccess" />
    </el-dialog>
  </div>
</template>

<script>
import { delDepartments, getDepartmentsList } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import addOrEdit from './deptDialog'
export default {
  name: 'DeptartName',
  components: { addOrEdit },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      originList: [],
      showDialog: false,
      curId: null,
      isEdit: false
    }
  },
  created() {
    this.loadDepartmentList()
  },
  methods: {
    async loadDepartmentList() {
      try {
        const res = await getDepartmentsList()
        // console.log(res)
        // 去掉数组中传智播客的第一条数组
        res.data.depts.shift()
        // 筛选出新数组
        this.originList = res.data.depts.map(({ id, pid, name, code }) => ({ id, pid, name, code }))
        // 将原数组转换成tree结构
        this.list = tranListToTreeData(res.data.depts)
      } catch (e) {
        this.$message.error(e.message)
      }
    },
    hAdd(id) {
      this.curId = id
      this.isEdit = false
      this.showDialog = true
    },
    hEdit(id) {
      this.curId = id
      this.isEdit = true
      this.showDialog = true
    },
    hSuccess() {
      this.showDialog = false
      this.isEdit = false
      this.loadDepartmentList()
    },
    async hDel(id) {
      await this.$confirm('您确定要删除吗？', '提示', { type: 'warning' })
      try {
        const res = await delDepartments(id)
        // console.log(res)
        this.$message.success(res.message)
        this.loadDepartmentList()
      } catch (e) {
        if (e === 'cancel') return
        this.$message.error(e.message)
      }
    },
    hClose() {
      this.isEdit = false
      this.showDialog = false
    }
  }
}
</script>
